// 复制方法
import {ElMessage} from "element-plus"
import {useRoute, useRouter} from "vue-router"
import {useStore} from "@/stores/store.js"
const route = useRoute()
const router = useRouter()

export function copy(copyContent) {
    try {
        if (navigator.clipboard) {
            navigator.clipboard.writeText(copyContent)
            console.log('复制成功')
            ElMessage.success('复制成功')
        } else {
            const textarea = document.createElement('textarea')
            textarea.value = copyContent
            document.body.appendChild(textarea)
            textarea.select()
            document.execCommand('copy')
            document.body.removeChild(textarea)
            console.log('复制成功（降级处理）')
            ElMessage.success('复制成功')
        }
    } catch (err) {
        console.error('复制失败：', err)
        ElMessage.error('复制失败')
    }
}
export function gotoUserInfo(router, account) {
    if (account == null) return
    if (account !== localStorage.getItem('account')) {
        router.push('/mine/like?account=' + account)
    } else {
        router.push('/mine/like')

    }
}
export function toMusicInfoPage(router, id, index, sheet) {
    // 跳个地址穿个id参数即可 音乐详细页面获取音乐的详细信息
    // router.push("/songDetail/" + id)
    useStore().togetherRoomInfo.togetherPlayMusicIndex = index
    useStore().togetherRoomInfo.togetherPlayMusicSheet = sheet

    router.push(`/songDetail/${id}?index=${index}&sheet=${sheet}`).then(r => console.log('success'))


}
export function sendWsRequest(websocket, message) {
    if (websocket && websocket.readyState === WebSocket.OPEN) {
        console.log(JSON.stringify(message))
        websocket.send(JSON.stringify(message))
    } else {
        console.error('WebSocket已关闭')
    }
}

export function goSingletonPage(url) {
    const pageName = "SingletonPage:" + url
    let existingWindow;
    for (let i = 0; i < window.length; i++) {
        if (window[i].name === pageName) {
            existingWindow = window[i];
            break;
        }
    }
    if (existingWindow) {
        existingWindow.focus();
    } else {
        const newWindow = window.open(url, pageName);
    }
}
